/**
 * Copyright 2010 ArcBees Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.arcbees.hive.client.home.blog;

import com.arcbees.hive.client.home.blog.BlogPresenter.MyView;
import com.arcbees.hive.client.home.blog.ui.BlogItemWidget;
import com.arcbees.hive.client.home.blog.ui.BlogItemWidgetFactory;
import com.arcbees.hive.client.mvp.ViewWithUiHandlers;
import com.arcbees.hive.client.mvp.strategies.UiHandlersStrategy;
import com.arcbees.hive.shared.home.blog.BlogItem;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.logical.shared.AttachEvent.Handler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

import com.gwtplatform.mvp.client.UiHandlers;

import java.util.List;

/**
 * @author Christian Goudreau
 */
public class BlogView extends ViewWithUiHandlers<BlogView.MyUiHandlers>
    implements MyView {
  /**
   * {@link BlogView}'s {@link UiBinder}.
   */
  public interface Binder extends UiBinder<Widget, BlogView> {
  }

  /**
   * {@link BlogView}'s {@link UiHandlers}.
   */
  public interface MyUiHandlers extends UiHandlers {
    void resize(Integer size);
  }

  @UiField
  HTMLPanel blogItemsLeftPanel;
  @UiField
  HTMLPanel blogItemsRightPanel;
  @UiField
  LayoutPanel blogPanel;
  @UiField
  HTMLPanel rssFeed;

  final Integer maxItems = 6;
  final Integer blogBottomTopMargin = 26;
  final Integer blogItemMarginCorrection = 32;
  final Integer animationDelay = 500;
  final Integer blogItemHeight = 80;
  final Double blogTop = 0D;
  private final BlogItemWidgetFactory blogItemWidgetFactory;
  private HandlerRegistration registration;

  @Inject
  public BlogView(final Binder uiBinder,
      final BlogItemWidgetFactory blogItemWidgetFactory,
      final UiHandlersStrategy<MyUiHandlers> uiHandlersStrategy) {
    super(uiHandlersStrategy);

    initWidget(uiBinder.createAndBindUi(this));

    this.blogItemWidgetFactory = blogItemWidgetFactory;
  }

  @Override
  public void setBlogItems(final List<BlogItem> blogItems) {
    if (asWidget().isAttached() && blogItems != null && blogItems.size() > 0) {
      Integer totalOffset = 0;
      Boolean alternate = true;

      for (Integer i = 0; i < blogItems.size() && i < maxItems; i++) {
        BlogItem blogItem = blogItems.get(i);

        BlogItemWidget blogItemWidget = blogItemWidgetFactory.create(blogItem);

        if (alternate) {
          blogItemsLeftPanel.add(blogItemWidget,
              blogItemsLeftPanel.getElement());
        } else {
          blogItemsRightPanel.add(blogItemWidget,
              blogItemsRightPanel.getElement());
        }

        alternate = !alternate;

        totalOffset += blogItemHeight + blogItemMarginCorrection;
      }

      // Small adjustments since the for loop add the correction too much times.
      totalOffset -= blogItemMarginCorrection;

      blogPanel.setWidgetTopHeight(blogItemsLeftPanel, blogTop, Unit.PX,
          totalOffset, Unit.PX);
      blogPanel.setWidgetTopHeight(blogItemsRightPanel, blogTop, Unit.PX,
          totalOffset, Unit.PX);

      blogPanel.animate(animationDelay);

      Double panelHeightCorrection = totalOffset.doubleValue() + blogBottomTopMargin;

      getUiHandlers().resize(panelHeightCorrection.intValue());
    } else {
      registration = asWidget().addAttachHandler(new Handler() {
        @Override
        public void onAttachOrDetach(AttachEvent event) {
          registration.removeHandler();
          setBlogItems(blogItems);
        }
      });
    }
  }
}
